(ns build
  (:require [clojure.tools.build.api :as b]))

(def lib 'my/ura)

(def build-folder "target/")
(def deployDir "../deploy/")
(def version "0.1")
(def jarName (format "%s-%s.jar" (name lib) version))

(def class-dir "target/classes")
(def jar-file (str build-folder jarName))
(def uber-file (str build-folder jarName))

;; delay to defer side effects (artifact downloads)
(def basis (delay (b/create-basis {:project "deps.edn"})))

(defn clean
  "
  clj -T:build clean
  -T:build  指定了clj使用deps.edn中的alias build 作为基准配置。
  clean:    指定要执行的方法名，就是本方法
  "
  [_]
  (b/delete {:path build-folder}) ; removing artifacts folder with (b/delete)
  (println (format "Build folder \"%s\" removed" build-folder)))


(def targetLibPath (str build-folder "app/lib/"))

(defn copySrc
  "
  clj -T:build copySrc 将src目录copy到 target
  "
  [_]
  (b/copy-dir {:src-dirs ["src"]  :target-dir (str build-folder "app/src/")})
  (b/copy-dir {:src-dirs ["web"]  :target-dir (str build-folder "app/web/")})
  (b/copy-dir {:src-dirs ["deploy"]  :target-dir (str build-folder "app/")})
  )

(defn copyLib
  "
  clj -T:build copyLib 将所有依赖的jar包，copy到 targetLibPath
  "
  [_]
  (let [jars (:classpath @basis)
        jarf (keys jars)
        ]
    (doall
     (map (fn [jf]
            (if (clojure.string/ends-with? jf ".jar")
              (let [str jf
                    slashPos (clojure.string/last-index-of str "/")
                    fnx (clojure.core/subs str (inc slashPos))
                    tgt (clojure.core/str targetLibPath fnx)
                    ]
                (b/copy-file {:src jf :target tgt})
                )
              )
            )
          jarf
          )
     )
    )
  )

(defn createBin [_]
  (b/write-file {:path (str build-folder "app/bin/start.sh")
               :string "java -cp \"./lib/*:src\" clojure.main -i ./src/ura/core.clj -m ura.core"
               })
  )

(defn generateApp [_]
  (copySrc nil)
  (copyLib nil)
  (createBin nil)
  )



(defn build
  "clj -T:build build : build a jar file"
  [_]
  (b/write-pom {:class-dir class-dir
                :lib lib
                :version version
                :basis @basis
                :src-dirs ["src"]})
  
  (b/copy-dir {:src-dirs ["src" "resources"]
               :target-dir class-dir})

  (b/compile-clj {:basis @basis
                  :ns-compile '[ura.core]
                  :class-dir class-dir})
  
  ;; (b/jar {:class-dir class-dir
  ;;         :jar-file jar-file})
  (b/uber {:class-dir class-dir
           :uber-file uber-file
           :basis @basis
           :main 'ura.core})
  )


(defn deploy
  "clj -T:build deploy : build and collect all files to ../deploy and zip it"
  [_]
  (build nil)
  (b/copy-dir {:src-dirs ["deploy"] :target-dir deployDir})
  (b/copy-dir {:src-dirs ["web"] :target-dir (str deployDir "web")})
  (b/copy-dir {:src-dirs ["data"] :target-dir (str deployDir "data")})
  (b/copy-file {:src uber-file :target (str deployDir jarName)})
  (b/zip {:src-dirs [deployDir] :zip-file "../ura.tar.gz"})
  )
